<template>
    <div>
        <h3>添加公告</h3>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td><span style="color:red;">*</span>标题</td>
                    <td>
                        <input type="text" v-model="quinforr.telite">
                    </td>
                </tr>
                <tr>
                    <td>公共内容</td>
                    <td>
                        <textarea v-model="quinforr.nieRong">

                        </textarea>
                    </td>
                </tr>
                <tr>
                    <td>公共照片</td>
                    <td>
                        <input type="file" @change="img" />
                        <img
                        :src="quinforr.photo"
                        width="100"
                        height="100"
                        alt=""
                        v-if="quinforr.photo != ''"
                        />
                    </td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input
              type="radio"
              name="state"
              v-model="quinforr.stete"
              value="1"
            />启用<input
              type="radio"
              name="state"
              v-model="quinforr.stete"
              value="0"
            />禁用
                    </td>
                  
                </tr>
                <tr>
                    <td>类型</td>
                    <td>
                        <select v-model="quinforr.lie">
                            <option value="" checked>--请选择--</option>
                            <option value="服务协议">服务协议</option>
                            <option value="普通协议">普通协议</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="button" value="确定" @click="add">
                        <input type="button" value="取消" @click="qu">
                    </td>
                    <td></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import {reactive,ref,onMounted} from 'vue'
import {useRoute,useRouter} from 'vue-router'
const router=useRouter();
let route=useRoute();
let quinforr=reactive({
  "telite": "",
  "nieRong": "",
  "photo": "",
  "stete": 0,
  "lie": "",
  "createTime": "2025-01-06T03:36:29.173Z",
  "nameRen": "admin",
  "isDelete": false

})
 const add=()=>{
    if (quinforr.telite=="") {
    alert('标题不能为空!');
    return;
  }
    if (quinforr.telite == '' || quinforr.telite.length > 20) {
    alert('标题只能小于20个字符');
    return;
  }

  if (quinforr.nieRong == '' || quinforr.nieRong.length >= 50) {
    alert('公告内容不大于50个字');
    return;
  }
    axios({
        url:'https://localhost:7188/api/Gonggao/Add',
        method:'post',
        data:quinforr
    })
    .then((res)=>{
        console.log(res);
        alert("成功!");
        router.push("/gaongaoshow")
    })
    .catch((err)=>{
        console.log(err);
    })
 }
const img = (e: any) => {
  console.log(e);
  let obj = e.target.files[0];
  let f = new FormData();
  f.append('file', obj);
  axios({
    url: 'https://localhost:7188/api/MenDian/UpdateImg',
    method: 'post',
    data: f,
  })
    .then((res) => {
      console.log(res);
      if (res.data == '不是图片') {
        alert('上传的不是图片');
        return;
      }

      if (res.data == '大于2M') {
        alert('上传的图片大于2M');
        return;
      }
      quinforr.photo = res.data;
    })
    .catch((err) => {
      console.log(err);
    });
};
const qu=()=>{
    quinforr.telite="";
    quinforr.nieRong="";
    quinforr.photo="";
    quinforr.stete=0;
    quinforr.lie="";
}
</script>

<style scoped>

</style>